package com.simple.demo.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class NioClient {

    public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress(5000));
            ByteBuffer buffer = ByteBuffer.allocate(50);
            boolean flag = true;
            Scanner scn = new Scanner(System.in);
            while(flag){
                buffer.clear();
                System.out.print("please inupt :" );
                String inputMsg = scn.next();
                if(inputMsg.equals("bye")){
                    flag = false;

                }
                //发送
                inputMsg += "\n";
                buffer.put(inputMsg.getBytes());
                buffer.flip();
                socketChannel.write(buffer);
                buffer.clear();
                //接收
                int read = socketChannel.read(buffer);
                String responMsg = new String(buffer.array(),0,read);
                System.out.println("recive server msg is = " +responMsg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
